from django.db import models

from apps.member.models import Member
from apps.system.models import Upload
from utils.exceptions import CustomError
from apps.system.models import Area


class Store(models.Model):
    merchant = models.ForeignKey(Member, verbose_name='商家', on_delete=models.PROTECT, blank=True,
                                 related_name='store')
    name = models.CharField(max_length=20, verbose_name='店名')
    city = models.ForeignKey(Area, verbose_name='地区', on_delete=models.PROTECT, null=True, blank=True)
    describe = models.CharField(max_length=200, verbose_name='店铺主营范围', null=True, blank=True)
    thumbnail = models.ForeignKey(Upload, verbose_name='店铺logo', on_delete=models.PROTECT, related_name='thumbnail')
    images = models.ManyToManyField(Upload, verbose_name='店铺图片s', related_name='images', blank=True)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    address = models.CharField(max_length=200, verbose_name='地址')
    address_detail = models.CharField(max_length=200, verbose_name='详细地址')
    latitude = models.FloatField(verbose_name='纬度', default=0)
    longitude = models.FloatField(verbose_name='经度', default=0)
    delete = models.BooleanField(verbose_name='是否删除', default=False)

    class Meta:
        db_table = 'store'
        verbose_name = '商铺'

    @staticmethod
    def get_instance_by_id(id):
        try:
            id = int(id)
        except:
            raise CustomError('无效的Store_id')
        instance = Store.objects.filter(pk=id).first()
        if not instance:
            raise CustomError('未找到对应的商铺实例')
        return instance


class Coupon(models.Model):
    DISCOUNT = 1
    PREFERENTIAL = 2
    AMOUNT = 3
    VALUE_TYPE = (
        (DISCOUNT, '折扣'),
        (PREFERENTIAL, '优惠'),
        (AMOUNT, '金额')
    )
    TO_BE_USED = 1
    HAS_USED = 2
    LOSRE_EFFICACY = 3
    STSTUS_CHOICE = (
        (TO_BE_USED, '待使用'),
        (HAS_USED, '已使用'),
        (LOSRE_EFFICACY, '已失效')
    )
    status = models.PositiveSmallIntegerField(choices=STSTUS_CHOICE, verbose_name='状态', default=TO_BE_USED)
    title = models.CharField(max_length=200, verbose_name='优惠券名称')
    take_effect_time = models.DateTimeField(verbose_name='生效时间')
    lose_efficacy_time = models.DateTimeField(verbose_name='失效时间')
    value_type = models.PositiveSmallIntegerField(choices=VALUE_TYPE, verbose_name='优惠券类型', default=AMOUNT)
    value = models.CharField(max_length=50, verbose_name='优惠券价值')
    notes = models.CharField(max_length=200, verbose_name='备注', null=True, blank=True)
    merchant = models.ForeignKey(Member, verbose_name='商家', on_delete=models.PROTECT, blank=True)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    delete = models.BooleanField(verbose_name='是否删除', default=False)

    class Meta:
        db_table = 'coupon'
        verbose_name = '优惠券'
        ordering = ('-create_time',)

    @staticmethod
    def get_instance_by_id(id):
        try:
            id = int(id)
        except:
            raise CustomError('无效的coupon_id')
        instance = Coupon.objects.filter(pk=id).first()
        if not instance:
            raise CustomError('未找到对应的优惠券实例')
        return instance


class FocusStore(models.Model):
    member = models.ForeignKey(Member, verbose_name='用户', on_delete=models.PROTECT, blank=True)
    store = models.ForeignKey(Store, verbose_name='店铺', on_delete=models.PROTECT, blank=True)
    create_time = models.DateTimeField(verbose_name='关注时间', auto_now_add=True)

    class Meta:
        db_table = 'store_focus'
        verbose_name = '关注商铺'
        ordering = ('-create_time',)
